import { createRouter, createWebHistory,RouteRecordRaw,createWebHashHistory} from "vue-router";
 
// 1. 配置路由
const routes: Array<RouteRecordRaw> = [
  {
    path: "/", // 默认路由 home页面
    component: () => import("../views/Login.vue"),//注意文件名大小写对应
  },
  {
    path: "/pinia",
    component: () => import("../views/PiniaDemo.vue"),
  },
  {
    path: "/vueuse",
    component: () => import("../views/VueUse.vue"),
  },  
  {
    path: "/i18n",
    component: () => import("../views/I18n.vue"),
  },
  {
    path: "/test",
    component: () => import("../views/Test.vue"),
  }
 
];


// 2.返回一个 router 实列，为函数
const router = createRouter({
  history: createWebHistory(),//history 模式
  // history: createWebHashHistory(),//hash 模式，url有#标志
  routes,
});


//路由守卫
//阻止页面回退方法，在路由切换前添加导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/' && from.path !== '/') {//表示从其他页面跳转首页时
    // next({ path: '/home' }) //跳转指定页面
     next()//放行
  } else {
    next()//放行
  }
})

 
// 3.导出路由   去 main.ts 注册 router.ts
export default router